 package scripting.item;

 import client.MapleClient;
 import client.inventory.Item;
 import configs.ServerConfig;
 import java.util.Map;
 import java.util.WeakHashMap;
 import javax.script.Invocable;
 import javax.script.ScriptEngine;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import scripting.AbstractScriptManager;








 public class ItemScriptManager
   extends AbstractScriptManager
 {
/*  24 */   private static final Logger log = LogManager.getLogger();

/*  26 */   private static final ItemScriptManager instance = new ItemScriptManager();
/*  27 */   private final Map<MapleClient, ItemActionManager> ims = new WeakHashMap<>();

   public static synchronized ItemScriptManager getInstance() {
/*  30 */     return instance;
   }

   public void start(MapleClient c, int npc, Item item) {
     try {
/*  35 */       if (this.ims.containsKey(c)) {
/*  36 */         dispose(c);
         return;
       }
/*  39 */       if (c.getPlayer().isAdmin()) {
/*  40 */         c.getPlayer().dropMessage(5, "开始道具脚本 NPC：" + npc + " ItemId：" + item.getItemId());
       }
/*  42 */       Invocable iv = getInvocable("item/" + item.getItemId() + ".js", c, true);
/*  43 */       ScriptEngine scriptengine = (ScriptEngine)iv;
/*  44 */       ItemActionManager im = new ItemActionManager(c, npc, item, iv);
/*  45 */       if (iv == null) {
/*  46 */         c.getPlayer().dropMessage(5, "找不到道具脚本: " + npc + " 道具ID: " + item.getItemId() + " 当前所在地图: " + c.getPlayer().getMapId());
/*  47 */         dispose(c);
         return;
       }
/*  50 */       this.ims.put(c, im);
/*  51 */       scriptengine.put("im", im);
/*  52 */       scriptengine.put("it", im);
/*  53 */       c.getPlayer().setConversation(1);
/*  54 */       c.setClickedNPC();
       try {
/*  56 */         iv.invokeFunction("start", new Object[0]);
/*  57 */       } catch (NoSuchMethodException nsme) {
/*  58 */         iv.invokeFunction("action", new Object[] { Byte.valueOf((byte)1), Byte.valueOf((byte)0), Integer.valueOf(0) });
       }
/*  60 */     } catch (Exception e) {
/*  61 */       log.error("执行道具脚本失败 道具ID: (" + item.getItemId() + ")..NPCID: " + npc + ". 错误信息: ", e);
/*  62 */       dispose(c);
/*  63 */       notice(c, item.getItemId());
     }
   }

   public void action(MapleClient c, byte mode, byte type, int selection) {
/*  68 */     if (mode != -1) {
/*  69 */       ItemActionManager im = this.ims.get(c);
/*  70 */       if (im == null) {
         return;
       }
       try {
/*  74 */         if (im.pendingDisposal) {
/*  75 */           dispose(c);
         } else {
/*  77 */           c.setClickedNPC();
/*  78 */           im.getIv().invokeFunction("action", new Object[] { Byte.valueOf(mode), Byte.valueOf(type), Integer.valueOf(selection) });
         }
/*  80 */       } catch (Exception e) {
/*  81 */         int npcId = im.getNpc();
/*  82 */         int itemId = im.getItemId();
/*  83 */         log.error("执行NPC脚本出错 NPC ID : " + npcId + " 道具ID: " + itemId + " 错误信息: ", e);
/*  84 */         dispose(c);
/*  85 */         notice(c, itemId);
       }
     }
   }

   public void dispose(MapleClient c) {
/*  91 */     ItemActionManager im = this.ims.get(c);
/*  92 */     if (im != null) {
/*  93 */       this.ims.remove(c);
/*  94 */       c.removeScriptEngine(ServerConfig.WORLD_SCRIPTSPATH + "/item/" + im.getItemId() + ".js");
     }
/*  96 */     if (c.getPlayer() != null && c.getPlayer().getConversation() == 1) {
/*  97 */       c.getPlayer().setConversation(0);
     }
   }

   public ItemActionManager getIM(MapleClient c) {
/* 102 */     return this.ims.get(c);
   }

   private void notice(MapleClient c, int itemId) {
/* 106 */     c.getPlayer().dropMessage(1, "这个道具脚本是错误的，请联系管理员修复它.道具ID: " + itemId);
   }
 }


/* Location:              C:\PGX\Server.jar!\scripting\item\ItemScriptManager.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */